package org.mte.sak.sort.comparator.impl;

import java.util.Locale;

import org.mte.sak.sort.comparator.AbstractComparator;

/**
 * <p>Comparatore Stringa.</p>
 *
 * @author Marco Traversari
 * @version $Revision: 1.2 $ $Date: 2011-12-27 11:42:34 $
 *
 */
public class StringComparator extends AbstractComparator {
    
    /**
     * StringComparator
     */
	public StringComparator() {
	}

	/**
     * getValue
     * @param value
     * @param formatReference
     * @return il valore dell'oggetto.
     * @see org.dromedary.lib.sort.Comparator#getValue(java.lang.String, java.lang.Object)
	 */
    public Object getValue(String value, Object formatReference) {
		value = (value == null) ? "" : value;
		return value;
	}
    
	/**
	 * getValue
	 * @param value
	 * @param formatReference
	 * @param locale
	 * @return value
	 */
	public Object getValue(String value, Object formatReference, Locale locale) {
		return getValue(value, formatReference);
	}
    
    /**
     * compare
     * @param obj1
     * @param obj2
     * @return true, false.
     * @see org.dromedary.lib.sort.Comparator#compare(java.lang.Object, java.lang.Object)
     */
	public boolean compare(Object obj1, Object obj2) {
		boolean result = isAscending;
		if (compareOneToOne(obj1, obj2) > 0) {
			result = !isAscending;
		}
		return result;
	}
	
    /**
     * compareOneToOne
     * @param obj1
     * @param obj2
     * @return true, false.
     * @see org.dromedary.lib.sort.Comparator#compareOneToOne(java.lang.Object, java.lang.Object)
     */
	public int compareOneToOne(Object obj1, Object obj2) {
		String str1 = (String) obj1;
		String str2 = (String) obj2;
		return str1.compareTo(str2);
	}
}